Data Cleaning for Brain Plotting

By: Adam Li

These functions are here to extract the mat files for the ROIs and faces/vertices of a brain plot. I extract the data from the .mat and convert them to dataframes for easy saving into csv and json format.

The data can be correspondingly read into d3.js using a web server and then used to create interactive brain plots on a web browser.


In [8]:
import os.path
import pandas as pd
from IPython.display import display

import numpy as np
import scipy.io
import h5py

import matplotlib
from matplotlib import *
from matplotlib import pyplot as plt
import itertools
from mpl_toolkits.axes_grid1 import make_axes_locatable

# pretty charting
import seaborn as sns
sns.set_palette('muted')
sns.set_style('darkgrid')

%matplotlib inline

In [37]:
## SET EEGROOTDIR
eegHomeDir = '/Users/adam2392/Documents/MATLAB/Johns Hopkins/NINDS_Rotation/'
eegVolumeDir = '/Volumes/NIL_PASS/'

if os.path.exists(eegVolumeDir):
    eegRootDir = eegVolumeDir
elif os.path.exists(eegHomeDir):
    eegRootDir = eegHomeDir
else:
    print "eegRootDir has none."
print "EEG Root Dir is: ", eegRootDir

## LOAD IN BRAIN PLOTTING DATA
BRAINDIR = '../brain_plotting_code/'
brainMat = BRAINDIR + 'brain.mat'
roiMat = BRAINDIR + 'ROI.mat'

brain = scipy.io.loadmat(brainMat)
faces = brain['F']
vertices = brain['V']
print "Faces matrix: ", faces.shape
print "Vertices matrix: ", vertices.shape

roi = h5py.File(roiMat)
roi = roi['ROI']
roi = np.array(roi).T
print "ROI Matrix: ", roi.shape


EEG Root Dir is:  /Volumes/NIL_PASS/
Faces matrix:  (400000, 3)
Vertices matrix:  (200004, 3)
ROI Matrix:  (1441, 3)

In [39]:
## CONVERT DATA INTO DATAFRAMES
df_roi = pd.DataFrame(data=roi,
                      index=[range(0, len(roi))],
                      columns=['x', 'y', 'z'])
df_vertices = pd.DataFrame(data=vertices,
                          index=[range(0, len(vertices))],
                          columns=['x', 'y', 'z'])
df_faces = pd.DataFrame(data=faces,
                       index=[range(0, len(faces))],
                       columns=['v1', 'v2', 'v3'])
print df_vertices.shape
print df_faces.shape
print df_roi.shape
display(df_roi.head())
display(df_vertices.head())
display(df_faces.head())

## SAVE TO JSON/CSV, comment 1 out
OUTPUT_ROI = 'data/roi.csv'
OUTPUT_V = 'data/vertices.csv'
OUTPUT_F = 'data/faces.csv'

# save the dataframe 
df_roi.to_csv(OUTPUT_ROI)
df_vertices.to_csv(OUTPUT_V)
df_faces.to_csv(OUTPUT_F)

OUTPUT_ROI = 'data/roi.json'
OUTPUT_V = 'data/vertices.json'
OUTPUT_F = 'data/faces.json'
df_roi.to_json(OUTPUT_ROI)
df_vertices.to_json(OUTPUT_V)
df_faces.to_json(OUTPUT_F)


(200004, 3)
(400000, 3)
(1441, 3)
x y z
0 -16.709240 68.009659 7.994206
1 -7.628133 68.009659 17.686089
2 -7.445113 68.009659 3.415041
3 -28.076458 62.509659 6.001571
4 -25.202274 62.509659 16.700949
x y z
0 -24.872351 -2.496538 -17.487497
1 -31.661413 11.653711 56.273590
2 -23.876099 -38.742859 56.636925
3 -22.466244 -71.454536 -9.936863
4 -37.237583 12.870546 13.421244
v1 v2 v3
0 211 1 13
1 310 211 13
2 211 310 212
3 311 212 310
4 212 311 213

In [ ]: